#include "demo.h"
#include "codeports.h"

typedef struct
{
	float x, y, z, c, r, g, b, s; 
} Star;

#define deepness 5
#define stars (1 << 10)

void
DEMO_Init(DEMO *demo, BOX *box)
{
	unsigned int star;
	Star *universe;
	demo->box = box;
	demo->data = malloc(stars * sizeof(Star));
	universe = demo->data;
	for(star = 0; star < stars; star++)
	{
		universe[star].x = rand() % demo->box->info.width * 4 - demo->box->info.width * 2;
		universe[star].y = rand() % demo->box->info.height * 4 - demo->box->info.height * 2;
		universe[star].z = rand() % deepness + 1;
		universe[star].c = rand() % 255;
		universe[star].r = rand() % 255;
		universe[star].g = rand() % 255;
		universe[star].b = rand() % 255;
		universe[star].s = (rand() % 3 + 1) * 0.5f;
	}
}

void
DEMO_Frame(DEMO *demo)
{
	unsigned int star;
	Star *universe = demo->data;
	for(star = 0; star < stars; star++)
	{
		if((universe[star].z -= universe[star].s) <= 1.0f)
		{
			universe[star].x = rand() % demo->box->info.width * 4 - demo->box->info.width * 2;
			universe[star].y = rand() % demo->box->info.height * 4 - demo->box->info.height * 2;
			universe[star].z = rand() % deepness + 1;
			universe[star].c = rand() % 255;
			universe[star].r = rand() % 255;
			universe[star].g = rand() % 255;
			universe[star].b = rand() % 255;
			universe[star].s = (rand() % 3 + 1) * 0.1f;
		}
		switch(demo->box->info.bpp)
		{
			case 1:
				BOX_SetPixelColor(demo->box, demo->box->info.width / 2 + universe[star].x / universe[star].z, demo->box->info.height / 2 + universe[star].y / universe[star].z, universe[star].c);
				break;
			case 2:
			case 3:
			case 4:
				BOX_SetPixelRGB(demo->box, demo->box->info.width / 2 + universe[star].x / universe[star].z, demo->box->info.height / 2 + universe[star].y / universe[star].z, universe[star].r, universe[star].g, universe[star].b);
				break;
		}
	}
}

void
DEMO_Final(DEMO *demo)
{
	free(demo->data);
}
